﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class UserMofForm : Form
    {
        private bool addflag = false;

        public UserMofForm()
        {
            InitializeComponent();
        }

        private bool validatepassword(string s)
        {
            Regex reg = new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$");
            Match m = reg.Match(s);
            return m.Success;
        }

        private bool modifycheck()
        {
            if (this.checkBox1.Checked)
            {
                if (this.textBox9.Text == User.Password)
                //if (User.Userrole != "系统管理员" && this.textBox9.Text == User.Password)
                {
                    MessageBox.Show("新密码和旧密码不可以一样,请重新输入密码");
                    return false;
                }

                if (this.textBox9.Text != this.textBox10.Text)
                {
                    MessageBox.Show("密码和确认密码不一致,请重新输入密码");
                    return false;
                }
            }
            
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("请输入用户姓名");
                return false;
            }
            if (this.comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择性别");
                return false;
            }
            if (this.comboBox2.SelectedItem == null)
            {
                MessageBox.Show("请选择用户权限");
                return false;
            }
            return true;
        }

        private bool checklength()
        {
            if (this.textBox1.Text.Length > 50)
            {
                MessageBox.Show("用户号码不能超过50个字符");
                return false;
            }

            if (this.textBox2.Text.Length > 50)
            {
                MessageBox.Show("用户姓名不能超过50个字符");
                return false;
            }

            if (this.textBox3.Text.Length > 30)
            {
                MessageBox.Show("身份证号码不能超过30个字符");
                return false;
            }

            if (this.textBox4.Text.Length > 30)
            {
                MessageBox.Show("联系电话不能超过50个字符");
                return false;
            }

            if (this.textBox5.Text.Length > 30)
            {
                MessageBox.Show("电子邮箱不能超过50个字符");
                return false;
            }

            if (this.textBox6.Text.Length > 500)
            {
                MessageBox.Show("工作地址不能超过500个字符");
                return false;
            }

            if (this.textBox7.Text.Length > 500)
            {
                MessageBox.Show("家庭地址不能超过500个字符");
                return false;
            }

            if (this.textBox8.Text.Length > 500)
            {
                MessageBox.Show("备注不能超过1000个字符");
                return false;
            }

            if (this.checkBox1.Checked == true)
            {
    /*            if (this.textBox9.Text.Length > 50)
                {
                    MessageBox.Show("密码不能超过50个字符");
                    return false;
                }


                if (this.textBox10.Text.Length > 50)
                {
                    MessageBox.Show("确认密码不能超过50个字符");
                    return false;
                }*/

                if( !validatepassword(this.textBox9.Text))
                {
                    MessageBox.Show("密码长度为8到15，必须包含至少一个大写字母，小写字母和数字");
                    return false;
                }

                if( !validatepassword(this.textBox10.Text))
                {
                    MessageBox.Show("确认密码长度为8到15，必须包含至少一个大写字母，小写字母和数字");
                    return false;
                }

            }

            return true;
        }


        private void displayuser()
        {
            try
            {
                if (this.textBox1.Text == "")
                {
                    return;
                }

                string  userid = this.textBox1.Text;
                MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

                var query = from c in dbcontext.tb_user
                            where c.ID ==  userid
                            select c;

                if (query.Count() == 0)
                {
                    MessageBox.Show("查询的用户号码不存在");
                    return;
                }

                this.textBox2.Text = query.First().Name;
                switch (query.First().sex)
                {
                    case "男":
                        this.comboBox1.SelectedIndex = 0;
                        break;
                    case "女":
                        this.comboBox1.SelectedIndex = 1;
                        break;
                }

                switch (query.First().Role)
                {
                    case "系统管理员":
                        this.comboBox2.SelectedIndex = 0;
                        break;
                    case "系统操作员":
                        this.comboBox2.SelectedIndex = 1;
                        break;
                    case "系统用户":
                        this.comboBox2.SelectedIndex = 2;
                        break;
                }

                this.textBox3.Text = query.First().IDcard;
                this.dateTimePicker1.Checked = false;
                if (query.First().Birthday.ToString() != "")
                {
                    this.dateTimePicker1.Value = DateTime.Parse(query.First().Birthday.ToString());
                }
                this.textBox4.Text = query.First().Phone;
                this.textBox5.Text = query.First().email;
                this.textBox6.Text = query.First().Workaddress;
                this.textBox7.Text = query.First().Homeaddress;
                this.textBox8.Text = query.First().Comment;
                this.checkBox1.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.textBox1.Enabled = false;
        }

        private void modifyuser()
        {
            try
            {
                if (this.textBox1.Text == "")
                {
                    return;
                }

                string userid = this.textBox1.Text;
                MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

                var query = from c in dbcontext.tb_user
                            where c.ID == userid
                            select c;

                if (query.Count() == 0)
                {
                    MessageBox.Show("需要更新的用户号码不存在");
                    return;
                }

                if (modifycheck() == false)
                {
                    return;
                }

                if (checklength() == false)
                {
                    return;
                }

                tb_user item = dbcontext.tb_user.First(c => c.ID == userid);

                string[] modifydataorg = new string[12];
                string[] modifydatadest = new string[12];

                modifydataorg[0] = item.ID.ToString();
                modifydataorg[1] = item.Name;
                modifydataorg[2] = item.Password = item.Password;
                modifydataorg[3] = item.sex;
                modifydataorg[4] = item.Role;
                modifydataorg[5] = item.IDcard == null ? "无" : item.IDcard;

                if (item.Birthday == null)
                {
                    modifydataorg[6] = "无";
                }
                else
                {
                    string s = item.Birthday.ToString();
                    int endindex = s.IndexOf(' ');
                    s = s.Substring(0, endindex);
                    modifydataorg[6] = s;
                }

           //     modifydataorg[6] = item.Birthday == null ? "无" : item.Birthday.ToString();
                modifydataorg[7] = item.Phone == null ? "无" : item.Phone;
                modifydataorg[8] = item.email == null ? "无" : item.email;
                modifydataorg[9] = item.Workaddress == null ? "无" : item.Workaddress;
                modifydataorg[10] = item.Homeaddress == null ? "无" : item.Homeaddress;
                modifydataorg[11] = item.Comment == null ? "无" : item.Comment;



                item.Name = this.textBox2.Text;
                item.sex = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
                item.Role = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();

                if (this.checkBox1.Checked)
                {
                    item.Password = this.textBox9.Text;
                }

                if (this.checkBox2.Checked)
                {
                    item.Password = "888888";
                }

                if (this.textBox3.Text != "")
                {
                    item.IDcard = this.textBox3.Text;
                }

                if (this.dateTimePicker1.Checked)
                {
                    item.Birthday = this.dateTimePicker1.Value;
                }

                if (this.textBox4.Text != "")
                {
                    item.Phone = this.textBox4.Text;
                }

                if (this.textBox5.Text != "")
                {
                    item.email = this.textBox5.Text;
                }

                if (this.textBox6.Text != "")
                {
                    item.Workaddress = this.textBox6.Text;
                }

                if (this.textBox7.Text != "")
                {
                    item.Homeaddress = this.textBox7.Text;
                }

                if (this.textBox8.Text != "")
                {
                    item.Comment = this.textBox8.Text;
                }

                modifydatadest[0] = this.textBox1.Text;
                modifydatadest[1] = this.textBox2.Text;
                modifydatadest[2] = item.Password;
                modifydatadest[3] = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
                modifydatadest[4] = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();
                modifydatadest[5] = this.textBox3.Text == "" ? "无" : this.textBox3.Text;

                if (this.dateTimePicker1.Checked == false)
                {
                    modifydatadest[6] = "无";
                }
                else
                {
                    string s = this.dateTimePicker1.Value.ToString();
                    int endindex = s.IndexOf(' ');
                    s = s.Substring(0, endindex);
                    modifydatadest[6] = s;
                }
  //              modifydatadest[6] = this.dateTimePicker1.Checked == false ? "无" : this.dateTimePicker1.Value.ToString();
                modifydatadest[7] = this.textBox4.Text == "" ? "无" : this.textBox4.Text;
                modifydatadest[8] = this.textBox5.Text == "" ? "无" : this.textBox5.Text;
                modifydatadest[9] = this.textBox6.Text == "" ? "无" : this.textBox6.Text;
                modifydatadest[10] = this.textBox7.Text == "" ? "无" : this.textBox7.Text;
                modifydatadest[11] = this.textBox8.Text == "" ? "无" : this.textBox8.Text;

                string detail = UserLog.builddetailstring(UserLog.usermanagermodifyheader, "修改", modifydatadest, modifydataorg);

                try
                {
                    dbcontext.SubmitChanges();
                    UserLog.addusermanagerecord(User.UserID, User.Username, "修改", item.ID, item.Name, "成功", detail);
                    addflag = false;
                    User.Password = this.textBox9.Text;
                    MessageBox.Show("修改用户成功");
                }
                catch (Exception ex)
                {
                    UserLog.addusermanagerecord(User.UserID, User.Username, "修改", item.ID, item.Name, "失败", detail);
                    MessageBox.Show("修改用户失败");
                }
                this.textBox1.Enabled = true;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            modifyuser();
        }





        private void UserMofForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Checked = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            displayuser();
        }

        private void UserMofForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (addflag == false)
            {
                e.Cancel = false;
                return;
            }

            DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                button1_Click(sender, e);
                e.Cancel = true;

                if (addflag == false)
                {
                    e.Cancel = false;
                }
            }
            else if (r == DialogResult.Cancel)
            {
                e.Cancel = true;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.Enabled = true;
            addflag = false;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                addflag = true;
                this.checkBox2.Checked = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                addflag = true;
                this.checkBox1.Checked = false;
            }
        }
    }
}
